﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     Message业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class MessageBLL
    {
        private static MessageBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IMessageDao dao = null;

        private MessageBLL()
        {
            dao = MessageSqlMapDao.Instance;
        }

        public static MessageBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new MessageBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<Message> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<Message> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<Message> GetListByParameters(object TopCounts, Message message)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<Message> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="MessageID"></param>
        /// <returns></returns>
        public Message GetDetail(System.String messageid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(messageid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public object Add(Message message)
        {
            try
            {
                return dao.Add(message);
            }
            catch 
            {
                
                throw;
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public int Update(Message message)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(message);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="MessageID"></param>
        /// <returns></returns>
        public int Delete(System.String messageid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(messageid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public int Delete(Message message)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(message);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public object SendMessage(Message message)
        {
            try
            {
                daoManager.BeginTransaction();
                //设置信息对发件人和收件人的显示状态
                message.SenderState = (int)Common.Enum.auvState.Enabled;
                message.ReceiverState = (int)Common.Enum.auvState.Enabled;

                message.State = (int)Common.Enum.auvState.Enabled;
                message.IsDel = (int)Common.Enum.auvDel.NotDel;
                message.CreateDate = DateTime.Now;
                message.UpdateDate = DateTime.Now;
                message.BeginDate = DateTime.Now;
                message.EndDate = DateTime.Now;
                dao.Add(message);
                //添加信息后更新联系人列表中的联系人及最后一条信息

                ContactList contact = new ContactList();
                //发信人的联系人列表
                contact.UserID = message.Sender;
                contact.ContactUserID = message.Receiver;
                contact.LatestMessage = message.MessageID;
                contact.UpdateDate = DateTime.Now;

                BLL.ContactListBLL.Instance.Add(contact);

                //收信人的联系人列表
                contact.UserID = message.Receiver;
                contact.ContactUserID = message.Sender;
                contact.LatestMessage = message.MessageID;
                contact.UpdateDate = DateTime.Now;
                BLL.ContactListBLL.Instance.Add(contact);

                daoManager.CommitTransaction();
                return message.MessageID;
            }
            catch
            {
                daoManager.RollBackTransaction();
                throw;
            }
        }



        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public IList<Message> ShowMessage(string userid,string contactuserid,int pagesize,int pageindex,ref int recordcount)
        {
            try
            {
                Model.NetPager pager = new Model.NetPager();
                pager.TableName = "Message";
                pager.PrimaryKey = "MessageID";
                pager.PageIndex = pageindex;
                pager.PageSize = pagesize;
                pager.Order = "CreateDate asc,MessageID asc";
                pager.SortType = (int)Common.Enum.SortType.Multi;
                //查询条件
                pager.Where = @"(Sender='" + userid + "' and Receiver='" + contactuserid + "' and SenderState=1" +
                    ") or (Sender='" + contactuserid + "' and Receiver='" + userid + "' and ReceiverState=1)";

                IList<Model.Message> list = dao.GetPagedList(pager);
                //将未读信息设为已读
                BLL.MessageBLL.Instance.ReadMessage(list,userid);

                return list;
            }
            catch 
            {
                
                throw;
            }
        }
        /// <summary>
        /// 将消息的状态设为已读
        /// </summary>
        /// <param name="list"></param>
        public void ReadMessage(IList<Message> list,string receiver)
        {
            try
            {
                ArrayList unreadlist = new ArrayList();
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Receiver==receiver&&!list[i].IsRead)
                    {
                        unreadlist.Add(list[i]);
                    }
                }
                if (unreadlist!=null)
                {
                    if (unreadlist.Count > 0)
                    {
                        Hashtable ht = new Hashtable();
                        ht.Add("list", unreadlist);
                        ht.Add("isRead", true);
                        dao.UpdateList(ht);
                    }
                }
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        ///  获取某用户的未读信息
        /// </summary>
        /// <param name="p"></param>
        public IList<Model.Message> GetUnReadMessage(string userid)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("receiver",userid);
                ht.Add("isRead", 0);
                ht.Add("receiverState",(int)Common.Enum.auvState.Enabled);
                return dao.GetListByParameters(ht);
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary>
        /// 根据收件人和发件人查找信息
        /// </summary>
        /// <param name="sender">发件人</param>
        /// <param name="receiver">收信人</param>
        /// <param name="queryfor">该信息要显示给谁看</param>
        /// <returns></returns>
        public IList<Message> GetMessageBySenderAndReceiver(string sender, string receiver,string queryfor)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("sender", sender);
                ht.Add("receiver",receiver);
                if(queryfor==sender)
                {
                    ht.Add("senderState", Common.Enum.auvState.Enabled);
                }
                if (queryfor == receiver)
                {
                    ht.Add("receiverState", Common.Enum.auvState.Enabled);
                }
                return dao.GetListByParameters(ht);
            }
            catch 
            {
                
                throw;
            }
        }


        /// <summary>
        /// 批量删除自己发出的信息（senderState=0）
        /// </summary>
        /// <param name="receivelist"></param>
        public void DeleteReceiveList(IList<Message> receivelist)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("list", receivelist);
                ht.Add("messageState", "receiverState");
                dao.UpdateList(ht);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 批量删除自己收到的信息(receiverState=0)
        /// </summary>
        /// <param name="sendlist"></param>
        public void DeleteSendList(IList<Message> sendlist)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("list", sendlist);
                ht.Add("messageState", "senderState");
                dao.UpdateList(ht);
            }
            catch
            {

                throw;
            }
        }
    }
}
